//
//  OpenServiceViewController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-19.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "OpenServiceViewController.h"
#import "DataService.h"
#import "TestCellModel.h"

@interface OpenServiceViewController ()
{
    NSMutableDictionary *mDic;
    NSMutableDictionary *mdic;
}

@end

@implementation OpenServiceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"测试表";
        self.navigationItem.titleView = t;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    mdic = [NSMutableDictionary dictionary];
    
    __block OpenServiceViewController *this = self;
    self.testTbView.finishBlock = ^{
        [this _loadMoreData];
    };
    
    _dataList = [NSMutableArray array];
    mDic = [NSMutableDictionary dictionary];
    self.testTbView.refreshDelegate = self;

    [self loadTestData];
}

- (void)_loadMoreData {
    _dataList = [NSMutableArray array];
    _dateDataList = [NSArray array];
    [mDic removeAllObjects];
    [mdic removeAllObjects];
    [self loadTestData];
}

- (void)loadTestData
{
    int index = 0;
    if (self.dataList.count != 0) {
        for (NSArray * arr in self.dataList) {
            index += arr.count;
        }
    }
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/init/test_table/%d",index/10 + 1];
    __weak OpenServiceViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];
}

- (void)loadDataFinished:(NSDictionary *)result
{
    int count = 0;
    int count1 = 0;
    for (NSArray *arr in [[result objectForKey:@"list"] allValues]) {
        count += arr.count;
    }
    
    for (NSArray *arr in [[mDic objectForKey:@"list"] allValues]) {
        count1 += arr.count;
    }
    
    
    if (count < 10) {
        self.testTbView.isMore = NO;
    }else
    {
        self.testTbView.isMore = YES;
    }
    
    
    for (NSString *key in [result allKeys]) {
        if (![key isEqualToString:@"list"]) {
            [mDic setObject:[result objectForKey:key] forKey:key];
        }
    }
    
    
    for (NSString *key in [[result objectForKey:@"list"] allKeys]) {
        NSArray *arr = [[result objectForKey:@"list"] valueForKey:key];
        NSMutableArray *marr = [NSMutableArray array];
        [marr addObjectsFromArray:[[mDic objectForKey:@"list"] objectForKey:key]];
        [marr addObjectsFromArray:arr];
        [mdic setObject:marr forKey:key];
    }
    NSString *str = [NSString stringWithFormat:@"list"];
    [mDic setObject:mdic forKey:str];
    
    _dataList = [NSMutableArray array];

    NSDictionary *dic = [mDic objectForKey:@"list"];
    NSArray *allkeys = [dic allKeys];
    self.dateDataList = [allkeys sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *str in self.dateDataList) {
        NSMutableArray *mArray = [NSMutableArray array];
        NSArray *listArray = [dic objectForKey:str];
        for (NSDictionary *listDic in listArray) {
            TestCellModel *model = [[TestCellModel alloc] initContentWith:listDic];
            [mArray addObject:model];
        }
        [self.dataList insertObject:mArray atIndex:0];
    }
    self.testTbView.dataList = self.dataList;
    NSMutableArray *mmArray = [NSMutableArray arrayWithCapacity:self.dateDataList.count];
    for (int i = 0; i < self.dateDataList.count; i ++) {
        [mmArray insertObject:self.dateDataList[i] atIndex:0];
    }
    self.testTbView.dateDataList = mmArray;
    
    [self.testTbView reloadData];
    [self.testTbView doneLoadingTableViewData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView {
    NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
    if (![str isEqualToString:@"加载更多数据"]) {
        return;
    }else
    {
        [self loadTestData];
    }
}


@end
